<script type="text/javascript">
/* Page init setting for User */
var initUrl = "<?= Zend_Registry::get('baseUrl')?>/execution/query";
var initParam = "currentpage=1";
var columnName = new Array("executionid", "testcaseid", "headline", "version", "result");
var operate = "edit,delete";
var currentPage = "1";

$(document).ready(function() {
	fillData(initUrl, initParam, columnName, operate, currentPage);
	markDisabled(Array("edit", "add", "headline"));
	$("#platform").attr("selectedIndex", 1);
	$("#version").attr("selectedIndex", 1);
	$("#result").attr("selectedIndex", 1);
});

/* Fill data to action panel for Edit */
function goEdit(obj, isEdit) {
	var dataRow = $(obj).parent().parent().children();
	var param = "executionid=" + dataRow.eq(0).text();
	$.post(initUrl, param, function (data) {
		var json = eval(data);
    	var lenJson = json.length - 1;
    	if (lenJson == 1) {
        	$("#executionid").val(json[0]['executionid']);
        	$("#testcaseid").val(json[0]['testcaseid']);
        	$("#headline").val(json[0]['headline']);
        	$("#tester").val(json[0]['tester']);
        	$("#platform").val(json[0]['platform']);
        	$("#version").val(json[0]['version']);
        	$("#result").val(json[0]['result']);
        	$("#comment").val(json[0]['comment']);
    	}
    	else {
        	$("#msg").text("<?= $this->translate('message.common.nodata')?>");
    	}
	});
	if (isEdit) {
		markEnabled(Array("edit"));
		markDisabled(Array("executionid", "testcaseid"));
	}
	else {
		markDisabled(Array("edit"));
		markEnabled(Array("executionid", "testcaseid"));
	}
	$("#msg").text("<?= $this->translate('tip.common.action')?>");
	$("#actionPanel").show();
	location.href = "#top";
}

/* Edit existing data */
function doEdit() {
	var executionid = $.trim($("#executionid").val());
	var url = "<?= Zend_Registry::get('baseUrl')?>/execution/edit";
	var param = "executionid=" + executionid + 
				"&tester=" + $.trim($("#tester").val()) + 
				"&platform=" + $.trim($("#platform").val()) + 
				"&version=" + $.trim($("#version").val()) + 
				"&result=" + $.trim($("#result").val()) + 
				"&comment=" + $.trim($("#comment").val());
    $.post(url, param, function (data) {
    	switch (data) {
    	case "no_permission":
    		$("#msg").text("<?= $this->translate('message.common.noperm.edit')?>");
    		break;
    	case "1":
    		fillEdit(initUrl, "executionid=" + executionid, columnName, executionid);
    		$("#msg").text("<?= $this->translate('message.common.edit.successful')?>" + $("#executionid").val());
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.edit.failed')?>" + $("#executionid").val());
    		break;
        default:
    		$("#msg").text(data);
    	}
	});

    if ($("#result").val() == "Failed") {
		if(!confirm("<?= $this->translate('message.testcase.result.failed')?>"))
			return false;
		location.href = "<?= Zend_Registry::get('baseUrl')?>/defect/index/source/Testcase-" + $.trim($("#testcaseid").val());
	}
}

/* Delete data according to ID */
function doDelete(obj) {
	if(!confirm("<?= $this->translate('message.common.delete.confirm')?>"))
		return false;
	var dataRow = $(obj).parent().parent().children();
	var executionid = $.trim(dataRow.eq(0).text());
	var url = "<?= Zend_Registry::get('baseUrl')?>/execution/delete";
	var param = "executionid=" + executionid;
	$.post(url, param, function(data) {
		switch (data) {
		case "no_permission":
    		$("#msg").text("<?= $this->translate('message.common.noperm.delete')?>");
    		break;
    	case "1":
    		$("#msg").text("<?= $this->translate('message.common.delete.successful')?>" + executionid);
			dataRow.remove();
			$("#totalRecord").text($("#totalRecord").text() - 1);
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.delete.failed')?>" + executionid);
    		break;
    	default:
    		$("#msg").text(data);
    	}
	});
}

/* Search data */
function doSearch(isSearch, currentPage) {
	if (isSearch) {
		$("#cacheDiv").removeData("cacheData");
		var cacheArray = new Array();
		cacheArray['executionid'] = $.trim($("#executionid").val());
		cacheArray['testcaseid'] = $.trim($("#testcaseid").val());
		cacheArray['tester'] = $.trim($("#tester").val());
		cacheArray['platform'] = $.trim($("#platform").val());
		cacheArray['version'] = $.trim($("#version").val());
		cacheArray['result'] = $.trim($("#result").val());
		cacheArray['comment'] = $.trim($("#comment").val());
		$("#cacheDiv").data("cacheData", cacheArray);
	}
	var cacheData = $("#cacheDiv").data("cacheData");
	if (typeof(cacheData) == "undefined")
		var param = "currentpage=" + currentPage;
	else
		var param = "currentpage=" + currentPage +
				"&executionid=" + cacheData['executionid'] + 
		    	"&testcaseid=" + cacheData['testcaseid'] + 
		    	"&tester=" + cacheData['tester'] +
		    	"&platform=" + cacheData['platform'] +
		    	"&version=" + cacheData['version'] +
		    	"&result=" + cacheData['result'] +
		    	"&comment=" + cacheData['comment'];
	fillData(initUrl, param, columnName, operate, currentPage);
	$("#msg").text("<?= $this->translate('tip.common.action')?>");
}

/* Reset KE content */
function resetKE() {
	
}
</script>

<br />
<table class="table-action">
<thead>
<tr>
	<a name="top"></a>
	<td colspan="5">
	    <?= $this->translate('common.page.panel')?>:&nbsp;&nbsp;
	    <label id="hideShow" />
	</td>
</tr>
</thead>
<tbody id="actionPanel">
<tr>
	<td width="14%"><?= $this->translate('column.common.id')?>:</td>
	<td width="36%"><input type="text" id="executionid" class="short" title="<?= $this->translate('tip.common.forsearch')?>" /></td>
	<td width="15%"><?= $this->translate('column.execution.testcaseid')?>:</td>
	<td width="25%" align="right"><input type="text" id="testcaseid" maxlength="30" class="short" title="<?= $this->translate('tip.common.forsearch')?>" /></td>
	<td width="10%"></td>
</tr>
<tr>
	<td><?= $this->translate('column.execution.platform')?>:</td>
	<td>
	<select id="platform" class="short">
		<option value=""></option>
		<?php
	        foreach ($this->platform as $platform) {
	            echo '<option value="' , $platform['pfname'] , '">' , $platform['pfname'] , '</option>';
	        }
		?>
	</select>
	</td>
	<td><?= $this->translate('column.execution.version')?>:</td>
	<td align="right">
	<select id="version" class="short">
		<option value=""></option>
		<?php
	        foreach ($this->version as $version) {
	        	$versionBuild = $version['version'] . '.' . $version['build'];
	            echo '<option value="' , $versionBuild , '">' , $versionBuild , '</option>';
	        }
		?>
	</select>
	</td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.execution.tester')?>:</td>
	<td><input type="text" id="tester" maxlength="30" class="short" /></td>
	<td><?= $this->translate('column.execution.result')?>:</td>
	<td align="right">
	<select id="result" class="short">
		<option value=""></option>
		<?php
			$common = new Common();
		    $testResult = $common->parseDefinition('test-result');
			foreach ($testResult as $tr) {
	            echo '<option value="' , $tr , '">' , $tr , '</option>';
			}
		?>
	</select>
	</td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.execution.headline')?>:</td>
	<td colspan="3"><input type="text" id="headline" maxlength="250" class="long" /></td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.execution.comment')?>:</td>
	<td colspan="3"><input type="text" id="comment" maxlength="250" class="long" /></td>
	<td></td>
</tr>
</tbody>
<tfoot>
<tr>
	<td><?= $this->translate('common.page.operate')?>:</td>
	<td><span id="actionButton" /></td>
	<td colspan="3">
		<span id="msg" class="message"><?= $this->translate('tip.common.action')?></span>
		<span id="loading" class="loading"><img src="<?= Zend_Registry::get('baseDir')?>/Common/Image/loading.gif" /></span>
	</td>
</tr>
</tfoot>
</table>

<br />
<table class="table-data" id="dataTable">
<thead>
<tr>
    <th width="9%" class="id"><?= $this->translate('column.common.id')?></th>
    <th width="11%"><?= $this->translate('column.execution.testcaseid')?></th>
    <th width="41%"><?= $this->translate('column.execution.headline')?></th>
    <th width="13%"><?= $this->translate('column.execution.version')?></th>
    <th width="13%"><?= $this->translate('column.execution.result')?></th>
    <th width="13%"><?= $this->translate('column.common.operate')?></th>
</tr>
</thead>
<tbody id="dataPanel"></tbody>
<tfoot id="footer"></tfoot>
</table>